package com.health.db.dao.user.impl;

import java.util.ArrayList;
import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserAccountVoucherBillDao;
import com.health.db.mapper.UserAccountVoucherBillMapper;
import com.health.db.model.UserAccountVoucherBill;
import com.health.db.model.UserAccountVoucherBillExample;
import com.health.redis.RedisConstants;

public class UserAccountVoucherBillDaoImpl extends ServiceInterface implements
		IUserAccountVoucherBillDao {

	UserAccountVoucherBillMapper userAccountVoucherBillMapper;
 
	String  selectByAccountId=":accountId:";
	String  selectByRunFlag=":runFlag:";
	String count=":count:";
	String select=":select:";
	
	/**
	 * userAccountVoucherBill
	 * {
	 * 	 userid   id
	 *   runFlag  状态
	 *  }
	 */
	@Override
	public int countUserAccountVoucherBillSimple(UserAccountVoucherBill userAccountVoucherBill) {
		
		getRedisOperatiorLogic();
		String key=RedisConstants.UserAccountVoucherBillMapper+count+"selectByAccountId"+userAccountVoucherBill.getUserid()
				+selectByRunFlag+userAccountVoucherBill.getRunFlag();
		
		int result=-1; 
		
		Object object=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			//get data from redis
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		 }

	     if(null!=object){
	    	 if (!object.equals("")) {
	    	 result =   (Integer) object;

	    	 }
	    }
	     else
	     {
	    	 result =userAccountVoucherBillMapper.countUserAccountVoucherBillSimple(userAccountVoucherBill);
	    	 if(redisOperatiorLogic.isJedisIsConnected()){
	    		 redisOperatiorLogic.setEx(key, result, getOrderRedisGroupNo("1"));
	    	 }
	     }
	
		return result;
	}
	
	
	
	/**
	 * 
	 * 主键 RecID
	 *     更新record对象中含有userId
	 */
	@Override
	public int updateByPrimaryKeySelective(UserAccountVoucherBill record) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		
		String key=RedisConstants.UserAccountVoucherBillMapper+count+"selectByAccountId"+record.getUserid()+selectByRunFlag+"0";
		String key1=RedisConstants.UserAccountVoucherBillMapper+select+"selectByAccountId"+record.getUserid()+selectByRunFlag+"0";
		
		int result=userAccountVoucherBillMapper.updateByPrimaryKeySelective(record);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key,getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key1,getOrderRedisGroupNo("1"));
		}
		return result;
	}


	/**
	 * userAccountVoucherBill
	 * {
	 * 	 userid   id
	 *   runFlag  状态
	 *  }
	 */
	@Override
	public List<UserAccountVoucherBill> selectByExample(
			UserAccountVoucherBillExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.UserAccountVoucherBillMapper+select+"selectByAccountId"+example.getUserid()
				+selectByRunFlag+example.getRunFlag();
		
		
		List<UserAccountVoucherBill> result= new ArrayList<UserAccountVoucherBill>();
		
		Object object=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		 }

	     if(null!=object&&!object.equals("")){
	    	 result =   (List<UserAccountVoucherBill> ) object;
	     }
	     else
	     {
	    	 result =userAccountVoucherBillMapper.selectByExample(example);
	    	 if(redisOperatiorLogic.isJedisIsConnected()){
	    		 redisOperatiorLogic.setEx(key, result, getOrderRedisGroupNo("1"));
	    	 }
	     }
	
		return result;
		
	}

	@Override
	public int insert(UserAccountVoucherBill record) {
		// TODO Auto-generated method stub
		int result=-1;
		 
		result =userAccountVoucherBillMapper.insert(record);
 
		return result;
	}

	
	/**********************************************************************************************/
	public UserAccountVoucherBillMapper getUserAccountVoucherBillMapper() {
		return userAccountVoucherBillMapper;
	}

	public void setUserAccountVoucherBillMapper(
			UserAccountVoucherBillMapper userAccountVoucherBillMapper) {
		this.userAccountVoucherBillMapper = userAccountVoucherBillMapper;
	}


	@Override
	public boolean Locked(Long stuffId, int type) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		String key=RedisConstants.UserAccountVoucherBillMapper+":lock:"+stuffId;
		//锁定
		if (type==0) {
	
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			if (redisOperatiorLogic.exists(key, getOrderRedisGroupNo("1"))) {
				return  false;
			}else{
				redisOperatiorLogic.setEx(key,key, getOrderRedisGroupNo("1"));
				return true;
			}
		}else {
			return true;
		}
		//解锁
		}else{
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
			return true;
		}
	}
	
}
